<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>个人中心</title>
		<meta name="viewport" content="initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="//cdn.bootcss.com/weui/0.4.3/style/weui.min.css">
		<link rel="stylesheet" href="//cdn.bootcss.com/jquery-weui/0.8.3/css/jquery-weui.min.css">
		<link rel="stylesheet" href="../css/index.css" />
	</head>

	<body id="wode">
		<header>
			<img src="../img/gslogo.png" />
		</header>
		<section>
			<div>
				<input type="number" id="phone" placeholder="请输入您的手机号" />
				<button type="button" id="sendCode" class="yzm">获取验证码</button>
			</div>
			<div>
				<input type="number" placeholder="请输入验证码" id="verifycode" />
			</div>
			<a href="javascript:;" class="weui_btn weui_btn_primary login">登录</a>
			<p>还没有账号？
				<a href="register_a.html">注册</a>
			</p>
		</section>
		<script src="//cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
		<script src="//cdn.bootcss.com/jquery-weui/0.8.3/js/jquery-weui.min.js"></script>
		<script type="text/javascript" src="../js/basic.js"></script>
		<script>
			var ip_addr = getUrl();
			$(document).ready(function() {
//				if(GetQueryString("type") != 1) {
//					$.alert({
//						title: '温馨提示',
//						text: '您已经登录，无需重复登录！',
//						onOK: function() {
//							WeixinJSBridge.call('closeWindow');
//						}
//					});
//				}
				checkLogin();
				setBind();
			});

			function checkPhone(phone) {
				var reg = new RegExp("^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$", "g");
				return reg.exec(phone);
			}

			function setBind() {
				//获取验证码
				$("#sendCode").click(function() {
					var phone = $("#phone").val();
					if(!checkPhone(phone)) {
						$.toptip('请输入正确的手机号！', 'warning');
						return;
					}
					var asd = 60;
					/* 更改样式  */
					$("#sendCode").attr('disabled', 'disabled');
					$("#sendCode").text(asd + "s后再试");
					$("#sendCode").css({
						"color": "#333333",
						"background": "#fff",
						"border": "1px solid #04BE02"
					});

					//定时器和计时器
					timer = setInterval(function() {
						asd--;
						$("#sendCode").text(asd + "s后再试");

					}, 1000);
					setTimeout(function() {
						clearInterval(timer);
						/* 还原样式 */
						$("#sendCode").removeAttr('disabled');
						$("#sendCode").text("获取验证码");
						$("#sendCode").css({
							"color": "#FB4747",
							"border": "1px solid #04BE02"
						});
					}, 60000);
					sendLogVerifyCode(phone)
				});
				//登录
				$(".login").click(function() {
					var phone = $("#phone").val();
					var verifyCode = $("#verifycode").val();
					if(phone == '' || phone == null) {
						$.toptip('请输入手机号！', 'warning');
					} else if(verifyCode == '' || verifyCode == null) {
						$.toptip('请输入验证码！', 'warning');
					} else {
						$.ajax({
							type: 'post',
							dataType: "json",
							url: ip_addr + 'user/login ',
							data: {
								phone: phone,
								verifyCode: verifyCode
							},
							success: function(data) {
								if(data.code == 0) {
									$.toast("登录成功！");
									
									localStorage.removeItem("userMsg");
									localStorage.setItem("userMsg", JSON.stringify(data.data));
									
									
									
									setTimeout(function() {
										WeixinJSBridge.call('closeWindow');
									}, 500);

								} else {
									$.toast(data.msg, "cancel");
								}
							}
						});
					}
				});
			}

			function sendLogVerifyCode(phone) {
				//用户登录发送验证码	
				$.ajax({
					type: 'post',
					dataType: "json",
					url: ip_addr + 'user/sendLogVerifyCode',
					data: {
						phone: phone
					},
					success: function(data) {
						var result = data.data;
						var innerHTML = '';
						if(data.code == 0) {
							$.toptip('发送验证码成功！', 'success');
						
						} else {
							$.toptip(data.msg, 'error');
						}
					}
				});
			}
			
			//判断是否登录
			function checkLogin() {
				$.ajax({
					type: 'post',
					dataType: "json",
					url: ip_addr + 'user/checkLogin',
					success: function(data) {
						var result = data.data;
						var innerHTML = '';
						if(data.code == 0) {
						$.alert({
							title: '温馨提示',
							text: '您已经登录，无需重复登录！',
							onOK: function() {
								WeixinJSBridge.call('closeWindow');
							}
						});
						} else {
							$.toptip(data.msg, 'error');
						}
					}
				});
			}
		</script>
	</body>

</html>